const clonedeep = require('lodash.clonedeep')
const PackageManager = require('../core/ProjectPackageManager')

const VUE_PACK_SCRIPTS = {
  start: 'vue-cli-service serve',
  serve: 'vue-cli-service serve',
  build: 'vue-cli-service build'
}

const VUE_INIT_DEP = [
  'core-js',
  'axios'
  // "vue",
  // "vue-router",
  // "vuex",
  // "element-ui",
]

const VUE_CLI_DEP = [
  '@vue/cli-plugin-router',
  '@vue/cli-plugin-vuex',
  '@vue/cli-service'
  // "vue-template-compiler",
]

const ESLINT_CONFIG = {
  root: true,
  env: {
    node: true
  },
  extends: ['plugin:vue/essential', 'eslint:recommended'],
  parserOptions: {
    parser: '@babel/eslint-parser'
  },
  rules: {}
}

const pm = new PackageManager()

function getDevDep(features) {
  const dep = new Set(VUE_CLI_DEP)

  if (features.includes('babel')) {
    dep.add('@vue/cli-plugin-babel')
    dep.add('@babel/core')
  }

  if (features.includes('linter')) {
    ;[
      '@vue/cli-plugin-eslint',
      '@vue/eslint-config-prettier',
      'eslint',
      'eslint-config-prettier',
      'eslint-plugin-prettier',
      'eslint-plugin-vue',
      'prettier'
    ].forEach((v) => dep.add(v))
  }

  if (features.includes('babel') && features.includes('linter')) {
    ;['@babel/eslint-parser'].forEach((v) => dep.add(v))
  }

  if (features.includes('githook')) {
    ;[
      'husky',
      'lint-staged',
      'commitizen',
      'cz-conventional-changelog',
      '@commitlint/cli',
      '@commitlint/config-conventional'
    ].forEach((v) => dep.add(v))
  }

  ;[
    'sass',
    'sass-loader',
    'sass-resources-loader',
    '@babel/plugin-proposal-optional-chaining',
    'babel-plugin-component'
  ].forEach((v) => dep.add(v))
  return Array.from(dep)
}

async function initVuePlugins(pkg, options) {
  pkg.scripts = clonedeep(VUE_PACK_SCRIPTS)
  if (options.features.includes('linter')) {
    pkg.scripts['lint'] = 'vue-cli-service lint'
    pkg.scripts['prettier'] = 'prettier --write .'
  }

  let vue_dep = clonedeep(VUE_INIT_DEP)
  if (options.vueVersion === '2') {
    vue_dep.push('element-ui')

    for await (const dep of vue_dep) {
      let version = await pm.getRemoteVersion(dep)
      pkg.dependencies[dep] = `^${version}`
    }

    pkg.dependencies = Object.assign(pkg.dependencies, {
      vue: '^2.6.14',
      'vue-router': '^3.5.1',
      vuex: '^3.6.2'
    })
    pkg.devDependencies = Object.assign(pkg.devDependencies, {
      'vue-template-compiler': '^2.6.14'
    })
  } else {
    vue_dep = vue_dep.concat(['vue', 'vue-router', 'vuex', 'element-plus', 'vue-template-compiler', '@element-plus/icons-vue'])

    for await (const dep of vue_dep) {
      let version = await pm.getRemoteVersion(dep)
      pkg.dependencies[dep] = `^${version}`
    }
  }

  const devDep = getDevDep(options.features)
  for await (const dep of devDep) {
    let version = await pm.getRemoteVersion(dep)
    if (dep === 'eslint') version = '8.8.0'
    if (dep === 'sass') version = '1.32.7'
    if (dep === 'sass-loader') version = '12.0.0'
    pkg.devDependencies[dep] = `^${version}`
  }

  if (options.features.includes('githook')) {
    pkg['config'] = {
      commitizen: {
        path: './node_modules/cz-conventional-changelog'
      }
    }

    pkg.scripts['prepare'] = 'husky init'
    pkg.scripts['lint-staged'] = 'lint-staged'
  }

  return pkg
}

async function initVueCli(name, preset) {
  const pkg = {
    name,
    version: '0.1.0',
    private: true,
    scripts: {},
    dependencies: {},
    devDependencies: {},
    browserslist: ['> 1%', 'last 2 versions', 'not dead']
  }

  for await (const dep of VUE_CLI_DEP) {
    let version = await pm.getRemoteVersion(dep)
    pkg.devDependencies[dep] = `^${version}`
    preset.plugins[dep] = {}
  }

  return pkg
}

module.exports.loadPkgConfig = async function (name, preset) {
  if (preset.type === 'vue') {
    return await initVueCli(name, preset)
  } else {
  }
}

module.exports.loadPlugins = async function (pkg, preset) {
  if (preset.type === 'vue') {
    return await initVuePlugins(pkg, preset)
  } else {
  }
}
